//
//  UIButton+ZYButton.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/3.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "UIButton+ZYButton.h"

@implementation UIButton (ZYButton)

/*
 * 获取按钮的size
 */
+ (CGSize)getSizeFromButtonTitle:(NSString *)titile
                            Font:(UIFont *)font{
    CGSize btnSize = [titile boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return btnSize;
}

+ (instancetype)buttonWithFrame:(CGRect)frame target:(id)target action:(SEL)action bgc:(UIColor *)color title:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    [button setTitle:title forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    
    button.frame  = frame;
    [button addTarget:target action:action forControlEvents:(UIControlEventTouchUpInside)];
    
    // 切圆角
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 10;
    
    // 如果传入背景颜色就不显示边框
    if (color) {
        button.backgroundColor = color;
    }else {
        // 画边框
        button.layer.borderColor = [UIColor blackColor].CGColor;
        button.layer.borderWidth = 1.0;
    }
    
    return button;
}

@end
